package com.seio.edc.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.nova.edc.constant.FileConstant;
import com.seio.edc.common.Settings;

public class FileUtil {
	private static String path;
	private static FileUtil instance;

	public static synchronized FileUtil getInstance() {
		if (instance == null) {
			instance = new FileUtil();
		}
		return instance;
	}

	public static String getFilePath() {
		if (path == null || path.trim().equalsIgnoreCase("null")) {
			path = Settings.getStringValue("physical_path");// 取得配置路径。
		}
		return path;
	}

	/**
	 * 获取指定文件夹下的所有文件名
	 */
	public static List<String> getAllFileNames(String path) {
		List<String> filenames = new ArrayList<String>();
		File file = new File(path);
		File[] array = file.listFiles();

		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				if (array[i].isFile()) {
					filenames.add(array[i].getName());
				} else if (array[i].isDirectory()) {
					getAllFileNames(array[i].getPath());
				}
			}
		}

		return filenames;
	}

	public static String getPath(String model, String type) {
		String dirPath = FileConstant.ROOT_DIR + model + type + File.separator
				+ +(new Date().getTime());
		return dirPath;
	}

	public static File getDir(String requestPath, String path) {
		String dirPath = requestPath + path;
		if (!dirPath.endsWith(File.separator)) {
			dirPath = dirPath + File.separator;
		}
		File dir = new File(dirPath);
		if (dir.exists()) {
			return dir;
		} else {
			dir.mkdirs();
			return dir;
		}
	}
}
